package cn.iocoder.yudao.module.blog.controller.admin.module;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.module.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.module.BlogModuleDO;
import cn.iocoder.yudao.module.blog.convert.module.BlogModuleConvert;
import cn.iocoder.yudao.module.blog.service.module.BlogModuleService;

@Tag(name = "管理后台 - 论坛版块")
@RestController
@RequestMapping("/blog/module")
@Validated
public class BlogModuleController {

    @Resource
    private BlogModuleService moduleService;

    @PostMapping("/create")
    @Operation(summary = "创建论坛版块")
    @PreAuthorize("@ss.hasPermission('blog:module:create')")
    public CommonResult<Long> createModule(@Valid @RequestBody BlogModuleCreateReqVO createReqVO) {
        return success(moduleService.createModule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新论坛版块")
    @PreAuthorize("@ss.hasPermission('blog:module:update')")
    public CommonResult<Boolean> updateModule(@Valid @RequestBody BlogModuleUpdateReqVO updateReqVO) {
        moduleService.updateModule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除论坛版块")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:module:delete')")
    public CommonResult<Boolean> deleteModule(@RequestParam("id") Long id) {
        moduleService.deleteModule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得论坛版块")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:module:query')")
    public CommonResult<BlogModuleRespVO> getModule(@RequestParam("id") Long id) {
        BlogModuleDO module = moduleService.getModule(id);
        return success(BlogModuleConvert.INSTANCE.convert(module));
    }

    @GetMapping("/list")
    @Operation(summary = "获得论坛版块列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:module:query')")
    public CommonResult<List<BlogModuleRespVO>> getModuleList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogModuleDO> list = moduleService.getModuleList(ids);
        return success(BlogModuleConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得论坛版块分页")
    @PreAuthorize("@ss.hasPermission('blog:module:query')")
    public CommonResult<PageResult<BlogModuleRespVO>> getModulePage(@Valid BlogModulePageReqVO pageVO) {
        PageResult<BlogModuleDO> pageResult = moduleService.getModulePage(pageVO);
        return success(BlogModuleConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出论坛版块 Excel")
    @PreAuthorize("@ss.hasPermission('blog:module:export')")
    @OperateLog(type = EXPORT)
    public void exportModuleExcel(@Valid BlogModuleExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogModuleDO> list = moduleService.getModuleList(exportReqVO);
        // 导出 Excel
        List<BlogModuleExcelVO> datas = BlogModuleConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "论坛版块.xls", "数据", BlogModuleExcelVO.class, datas);
    }

}
